<?php
/**
 * 支付接口调测例子
 * ================================================================
 * index 进入口，方法中转
 * submitOrderInfo 提交订单信息
 * queryOrder 查询订单
 * 
 * ================================================================
 */
namespace weifutong\weixinsaoma;

//require('Utils.class.php');
//require('config/config.php');
//require('class/RequestHandler.class.php');
//require('class/ClientResponseHandler.php');
//require('class/PayHttpClient.class.php');
use weifutong\weixinsaoma\Utils;
use weifutong\weixinsaoma\config\Config;
use weifutong\weixinsaoma\core\RequestHandler;
use weifutong\weixinsaoma\core\ClientResponseHandler;
use weifutong\weixinsaoma\core\PayHttpClient;

Class Request{
    //$url = 'http://192.168.1.185:9000/pay/gateway';

    static $resHandler = null;
    static $reqHandler = null;
    static $pay = null;
    static $cfg = null;
    
    public function __construct(){
        self::Request();
    }

    public static function Request(){
        self::$resHandler = new ClientResponseHandler();
        self::$reqHandler = new RequestHandler();
        self::$pay = new PayHttpClient();
        self::$cfg = new Config();

        self::$reqHandler->setGateUrl(self::$cfg->C('url'));

        $sign_type = self::$cfg->C('sign_type');
        
        if ($sign_type == 'MD5') {
            self::$reqHandler->setKey(self::$cfg->C('key'));
            self::$resHandler->setKey(self::$cfg->C('key'));
            self::$reqHandler->setSignType($sign_type);
        } else if ($sign_type == 'RSA_1_1' || $sign_type == 'RSA_1_256') {
            self::$reqHandler->setRSAKey(self::$cfg->C('private_rsa_key'));
            self::$resHandler->setRSAKey(self::$cfg->C('public_rsa_key'));
            self::$reqHandler->setSignType($sign_type);
        }
    }

    
    /**
     * 提交订单信息
     */
    public static function submitOrderInfo(){
        self::$reqHandler->setReqParams($_POST,'submitOrderInfo');
        self::$reqHandler->setParameter('service','pay.weixin.native');//接口类型：pay.weixin.native  表示微信扫码
     // self::$reqHandler->setParameter('service','pay.alipay.native');//接口类型：pay.alipay.native  表示支付宝扫码
        // self::$reqHandler->setParameter('service','pay.jdpay.native');//接口类型：pay.jdpay.native   表示京东钱包扫码
        // self::$reqHandler->setParameter('service','pay.unionpay.native');//接口类型：pay.unionpay.native   表示银联钱包扫码
        self::$reqHandler->setParameter('mch_id',self::$cfg->C('mchId'));//必填项，商户号，由威富通分配
        self::$reqHandler->setParameter('version',self::$cfg->C('version'));
        self::$reqHandler->setParameter('sign_type',self::$cfg->C('sign_type'));
        // self::$reqHandler->setParameter('limit_credit_pay', '1');
        
        //通知地址，必填项，接收威富通通知的URL，需给绝对路径，255字符内格式如:http://wap.tenpay.com/tenpay.asp
        //$notify_url = 'http://'.$_SERVER['HTTP_HOST'];
	self::$reqHandler->setParameter('notify_url','http://dawei.dev.swiftpass.cn/payInterface_native_weixin/request.php?method=callback');
        self::$reqHandler->setParameter('nonce_str',mt_rand(time(),time()+rand()));//随机字符串，必填项，不长于 32 位
        self::$reqHandler->createSign();//创建签名
        
        $data = Utils::toXml(self::$reqHandler->getAllParameters());
        
        self::$pay->setReqContent(self::$reqHandler->getGateURL(),$data);
        if(self::$pay->call()){
            self::$resHandler->setContent(self::$pay->getResContent());
            self::$resHandler->setKey(self::$reqHandler->getKey());
            if(self::$resHandler->isTenpaySign()){
                //当返回状态与业务结果都为0时才返回支付二维码，其它结果请查看接口文档
                if(self::$resHandler->getParameter('status') == 0 && self::$resHandler->getParameter('result_code') == 0){
                    echo json_encode(array('code_img_url'=>self::$resHandler->getParameter('code_img_url'),
                                           'code_url'=>self::$resHandler->getParameter('code_url'),
                                           'code_status'=>self::$resHandler->getParameter('code_status'),
                                           'type'=>self::$reqHandler->getParameter('service')), JSON_UNESCAPED_SLASHES);
                    exit();
                }else{
                    echo json_encode(array('status'=>500,'msg'=>'Error Code:'.self::$resHandler->getParameter('err_code').' Error Message:'.self::$resHandler->getParameter('err_msg')));
                    exit();
                }
            }
            echo json_encode(array('status'=>500,'msg'=>'Error Code:'.self::$resHandler->getParameter('status').' Error Message:'.self::$resHandler->getParameter('message')));
        }else{
            echo json_encode(array('status'=>500,'msg'=>'Response Code:'.self::$pay->getResponseCode().' Error Info:'.self::$pay->getErrInfo()));
        }
    }

}
?>